#!/usr/bin/env node

/**
 * 测试网络连通性脚本
 * 验证真机是否能访问后端服务
 */

const http = require('http');
const https = require('https');

console.log('🌐 测试网络连通性');
console.log('==================');

const testUrls = [
  'http://192.168.174.112:3000',
  'http://localhost:3000',
  'http://127.0.0.1:3000'
];

const testImageUrls = [
  'http://192.168.174.112:3000/uploads/pcproductimg/pc_1757685895753_350145138.jpg',
  'http://192.168.174.112:3000/uploads/pcproductimg/pc_1757662705846_516420226.jpg'
];

// 测试HTTP连接
function testHttpConnection(url) {
  return new Promise((resolve) => {
    const startTime = Date.now();
    
    const req = http.get(url, (res) => {
      const endTime = Date.now();
      const responseTime = endTime - startTime;
      
      console.log(`✅ ${url} - 状态码: ${res.statusCode}, 响应时间: ${responseTime}ms`);
      resolve({ success: true, statusCode: res.statusCode, responseTime });
    });
    
    req.on('error', (error) => {
      const endTime = Date.now();
      const responseTime = endTime - startTime;
      
      console.log(`❌ ${url} - 连接失败: ${error.message}, 响应时间: ${responseTime}ms`);
      resolve({ success: false, error: error.message, responseTime });
    });
    
    req.setTimeout(5000, () => {
      req.destroy();
      console.log(`⏰ ${url} - 连接超时`);
      resolve({ success: false, error: 'timeout' });
    });
  });
}

// 测试图片文件访问
function testImageAccess(url) {
  return new Promise((resolve) => {
    const startTime = Date.now();
    
    const req = http.get(url, (res) => {
      const endTime = Date.now();
      const responseTime = endTime - startTime;
      
      if (res.statusCode === 200) {
        console.log(`🖼️ ${url} - 图片可访问, 状态码: ${res.statusCode}, 响应时间: ${responseTime}ms`);
        resolve({ success: true, statusCode: res.statusCode, responseTime });
      } else {
        console.log(`❌ ${url} - 图片不可访问, 状态码: ${res.statusCode}, 响应时间: ${responseTime}ms`);
        resolve({ success: false, statusCode: res.statusCode, responseTime });
      }
    });
    
    req.on('error', (error) => {
      const endTime = Date.now();
      const responseTime = endTime - startTime;
      
      console.log(`❌ ${url} - 图片访问失败: ${error.message}, 响应时间: ${responseTime}ms`);
      resolve({ success: false, error: error.message, responseTime });
    });
    
    req.setTimeout(5000, () => {
      req.destroy();
      console.log(`⏰ ${url} - 图片访问超时`);
      resolve({ success: false, error: 'timeout' });
    });
  });
}

// 主测试函数
async function runTests() {
  console.log('\n📡 测试后端服务连通性:');
  console.log('========================');
  
  for (const url of testUrls) {
    await testHttpConnection(url);
  }
  
  console.log('\n🖼️ 测试图片文件访问:');
  console.log('====================');
  
  for (const url of testImageUrls) {
    await testImageAccess(url);
  }
  
  console.log('\n💡 诊断建议:');
  console.log('============');
  console.log('1. 如果所有连接都失败，检查后端服务是否启动');
  console.log('2. 如果localhost成功但192.168.174.112失败，检查网络配置');
  console.log('3. 如果服务连接成功但图片失败，检查图片文件是否存在');
  console.log('4. 如果连接超时，检查防火墙设置');
  
  console.log('\n🔧 可能的解决方案:');
  console.log('==================');
  console.log('1. 确保后端服务在 192.168.174.112:3000 上运行');
  console.log('2. 检查Windows防火墙是否阻止了3000端口');
  console.log('3. 确保真机和电脑在同一网络下');
  console.log('4. 尝试在真机浏览器中直接访问 http://192.168.174.112:3000');
}

// 运行测试
runTests().catch(console.error);
